package cn.dong.edu.controller.api;

import cn.dong.api.domain.CommonResult;
import cn.dong.base.dto.CourseDto;
import cn.dong.edu.domain.Course;
import cn.dong.edu.domain.dto.WebCourseVo;
import cn.dong.edu.service.CourseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RequestMapping("/api/edu/course")
@Slf4j
@RestController
public class ApiCourseController {


    @Autowired
    private CourseService courseService;

    /**
     * 根据条件分类查询课程
     * @param webCourseVo
     * @return
     */
    @GetMapping("/findByCondition/{current}/{size}")
    public CommonResult findByCondition(
            @ApiParam(value = "查询对象") WebCourseVo webCourseVo,
            @PathVariable Integer current,
            @PathVariable Integer size
    ) {
        IPage<Course> list = courseService.findByCondition(current, size, webCourseVo);
        return CommonResult.ok().data(list);
    }

    /**
     * 根据课程id，获取课程详细信息，包括章节
     * @return
     */
    @GetMapping("/findDetailedInfo/{id}")
    public CommonResult<Map<String, Object>> findDetailedInfo(@PathVariable String id){
        Map<String, Object> map = courseService.findDetailedInfo(id);
        return CommonResult.ok().data(map);
    }


    /**
     * 根据课程id，查询课程信息，返回Dto对象，用于订单服务获取信息
     * @return
     */
    @GetMapping("/inner/get-course-dto/{id}")
    public CourseDto getCourseDto(@PathVariable String id) {
        return courseService.getCourseDto(id);
    }



    /**
     * 更改订单销量
     * @param id
     */
    @GetMapping("/api/edu/course/update-buy-count/{id}")
    CommonResult updateBuyCount(@PathVariable("id") String id){
        courseService.updateBuyCount(id);
        return CommonResult.ok();
    }

}
